﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars;
using DevExpress.Utils.Controls;

namespace QuanLyThuNgan
{
    public class PhanQuyen
    {
        private static int idChuongTrinh;
        private static int idNguoiDung;

        public static DataTable MainMenu(int idct, int idnguoidung)
        {
            string sql = string.Format("exec dbo.usp_PhanQuyen_PhanQuyenChucNang {0},{1}", idct, idnguoidung);
            return Uit.it_SQL.getDataTable(sql);
        }
        public static DataTable FormMenu(int idct, int idnguoidung, string FormMenu)
        {
            string sql = string.Format("exec dbo.usp_PhanQuyen_PhanQuyenChucNangChiTiet {0},{1},'{2}'", idct, idnguoidung, FormMenu);
            return Uit.it_SQL.getDataTable(sql);
        }
        public static void LoadFrmMain(RibbonControl ribbon,int idct,int idnd)
        {
            idChuongTrinh = idct;
            idNguoiDung = idnd;
            DataTable dtMenupage = MainMenu(idChuongTrinh,idNguoiDung);
            foreach (DataRow dr in dtMenupage.Rows)
            {
                if (dr["QuyenChucNang"].ToString().Trim() == "0")
                {
                    if (dr["CapChucNang"].ToString().Trim() == "1")
                        FindItemByNamePages(ribbon, dr["MenuName"].ToString(), false);
                    else
                        FindItemByName(ribbon, dr["MenuName"].ToString());
                }
            }
        }
        public static void LoadForm(Control ctrl)
        {
            DataTable dtFormMenu = FormMenu(idChuongTrinh,idNguoiDung,ctrl.Name );
            foreach (DataRow dr in dtFormMenu.Rows)
            {
                ResetForm(ctrl, dr["TenDoiTuong"].ToString().Trim());
            }
        }
     
        public static void FindItemByName(RibbonControl ribbon, string name)
        {
            foreach (BarItem item in ribbon.Items)
            {
                if (item.Name.Trim() == name.Trim())
                    item.Enabled = false;
                // item.Visibility = BarItemVisibility.Never;
            }

        }
        public static void FindItemByNamePages(RibbonControl ribbon, string name, bool tt)
        {
            foreach (RibbonPage page in ribbon.Pages)
            {
                if (page.Name == name)
                {
                    page.Visible = tt;
                }
            }
        }
        public static void ResetForm(Control ctrl, string name)
        {
            foreach (Control i in ctrl.Controls)
            {
                if (i.Name == name)
                    i.Enabled = false;
               ResetForm(i, name);
            }
        }
        
    }
   
}
